import { FC, useState, useEffect } from 'react';
import { Space, Form, Input, Button, Checkbox } from 'antd';
import CardSpace from '../components/CardSpace';
import { UserOutlined, LockOutlined } from '@ant-design/icons';

const Login: FC = () => {
  // form是一个对象，有一个地址指向，form对象下面有很多的方法
  const [form] = Form.useForm();
  const [form2] = Form.useForm();

  // console.log(form);

  // 当Form组件的submit按钮被点击时触发
  // const onFinish = (values: any) => {
  //   console.log('Finish:', values);
  //   console.log(form.getFieldsValue());
  //   // console.log(form.getFieldValue('username'));
  //   // console.log(form.getFieldError('username'));

  //   // form.resetFields();
  // };

  const fn = () => {
    // console.log(form.getFieldsValue());
    // console.log(form.getFieldsError());
    form
      .validateFields()
      .then(() => {
        console.log(form.getFieldsValue());
      })
      .catch((err) => {
        console.log(err);
      });
  };

  const onFinish2 = (values: any) => {
    console.log('Received values of form: ', values);
  };

  return (
    <Space direction="vertical" style={{ display: 'flex' }}>
      <CardSpace title="登录行内表单">
        {/* 表单所有东西需要再Form组件里面 */}
        <Form
          form={form}
          name="horizontal_login"
          // 横排
          layout="inline"
          // onFinish={onFinish}
        >
          {/* username */}
          <Form.Item
            // 里面的name属性就是最后拿到的那个对象的key
            name="username"
            // 规则
            rules={[
              { required: true, message: 'Please input your username!' },
              {
                max: 5,
                message: 'maxmax',
              },
            ]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>

          <Form.Item
            // 里面的name属性就是最后拿到的那个对象的key
            name="phone"
            // 规则
            rules={[
              { required: true, message: 'Please input your phone!' },
              {
                pattern: /^1[3456789]\d{9}$/,
                message: '这不是一个手机号',
              },
            ]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          {/* password */}
          <Form.Item
            name="password"
            rules={[{ required: true, message: 'Please input your password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          {/* <Form.Item shouldUpdate>
            <Button
              type="primary"
              htmlType="submit"
              // disabled={
              //   !form.isFieldsTouched(true) ||
              //   !!form.getFieldsError().filter(({ errors }) => errors.length)
              //     .length
              // }
            >
              Log in
            </Button>
          </Form.Item> */}
        </Form>

        <Button onClick={fn}>btn</Button>
      </CardSpace>

      <CardSpace title="登录表单">
        <Form
          name="normal_login"
          className="login-form"
          initialValues={{ remember: true }}
          onFinish={onFinish2}
          form={form2}
        >
          <Form.Item
            name="username"
            rules={[{ required: true, message: 'Please input your Username!' }]}
          >
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item
            name="password"
            rules={[{ required: true, message: 'Please input your Password!' }]}
          >
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          <Form.Item>
            <Form.Item name="remember" valuePropName="checked" noStyle>
              <Checkbox>Remember me</Checkbox>
            </Form.Item>

            <a className="login-form-forgot" href="">
              Forgot password
            </a>
          </Form.Item>

          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              className="login-form-button"
            >
              Log in
            </Button>
            Or <a href="">register now!</a>
          </Form.Item>
        </Form>
      </CardSpace>
    </Space>
  );
};

export default Login;
